package com.example.cloud.controller;

import com.example.cloud.service.PaymentService;
import com.example.comment.config.Result;
import com.example.comment.entity.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author kuang
 * @Date 2021/7/11 18:09
 */
@RestController
@Slf4j
@RequestMapping(value = "/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;
    //服务发现客户端
    @Resource
    private DiscoveryClient client;

    @GetMapping(value = "/query/{id}")
    public Result<Payment> queryPayment(@PathVariable("id") Long id) {
        log.info("请求参数：{}", id);
        Result<Payment> result = new Result<>();
        Payment payment = paymentService.queryPayment(id);
        log.info("查询结果：{}", payment);
        if (payment != null) {
            result.setCode(200);
            result.setMessage("查询成功" + serverPort);
            result.setData(payment);
        } else {
            result.setCode(400);
            result.setMessage("没有" + id + "记录！");
            result.setData(null);
        }
        return result;
    }

    @PostMapping(value = "/add")
    public Result<Integer> addPayment(@RequestBody Payment payment) {
        int result = paymentService.addPayment(payment);
        log.info("新增支付：{}", payment);
        if (result > 0) {
            return new Result<>(200, "新增成功" + serverPort, result);
        } else {
            return new Result<>(400, "新增失败", null);
        }
    }

    @PostMapping(value = "/modify")
    public Result<Integer> modifyPayment(@RequestBody Payment payment) {
        log.info("数据修改：{}", payment);
        int result = paymentService.modifyPayment(payment);
        if (result > 0) {
            return new Result<>(200, "修改成功" + serverPort, result);
        } else {
            return new Result<>(400, "修改失败", null);
        }
    }

    @PostMapping(value = "/delete/{id}")
    public Result<Long> deletePayment(@PathVariable("id") Long id) {
        log.info(" 删除ID：{}", id);
        int result = paymentService.deletePayment(id);
        if (result > 0) {
            return new Result<>(200, "删除成功！" + serverPort, null);
        } else {
            return new Result<>(400, "删除失败！", id);
        }
    }

    @GetMapping(value = "/discovery")
    public Object discover() {
        List<String> services = client.getServices();
        for (String service : services) {
            log.info("注册服务：{}", service);
        }
        List<ServiceInstance> instances = client.getInstances("payment");
        for (ServiceInstance instance : instances) {
            log.info("服务实例：{}", instance.getInstanceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }
        return this.client;
    }

    @GetMapping(value = "/port")
    public String getServerPort() {
        return serverPort;
    }

    @GetMapping(value = "/feign/timeout")
    public String timeOut() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }
}
